
   $("#edit_id").hide();
    $(".addtext").hide()
    $(".recycle").hide()
    $("#list_art").addClass("pitch")
    $("#classify").hide()

//富文本

 if(true){
   
 }
    //点击添加文件,查询分类列表
    $(".add").click(function () {
        $(".addtext").show().removeClass("ativeout").addClass("ative");
        $(".equi").click();
      
        $.post("/getCate",function(data){
      
            catData=data.result
            let str =` <option value="0">所属分类</option>\n `
            catData.forEach(item =>{
                str +=`<option value="${item.article_id}">${item.classify}</option>\n `
            })
            $("#classcat2").html(str)
        })
     
    })
    var ii;
    //获取编辑数据
    $("#inquiry").on("click",".edit",getEditData)
   function getEditData() {
        let xhq = new XMLHttpRequest(); 
        let edit_id = this.getAttribute("edit_id")
        xhq.onreadystatechange = function () {
            if (this.readyState === 4 && this.status === 200) {
                let{ artitem ,catData}=JSON.parse( this.responseText);
                let str =` <option value="0">所属分类</option>\n `
                catData.forEach(item =>{
                    str +=`<option value="${item.article_id}">${item.classify}</option>\n `
                })
                $(".upda").removeClass("ativeout").show().addClass("ative")
                $("#edit_title").val(artitem.title);
                $("#classify3").html(str)
                $("#classify3").val(artitem.classify2);
                $("#edit_author").val(artitem.author);
                // $("#edit_content").val(artitem.content);
                editor.txt.html(artitem.content)
                $("#edit_id").val(artitem.art_id);
                $("#fo_url").val(artitem.img_url); 
                [...$("input[name='fo_status']")].find(item => item.value == artitem.status).checked = true;
                $(".ylimg")[0].src ="";  
            }
        }
        xhq.open("get", `/inquiry?edit_id=${edit_id}`, true)
        xhq.send();

    }
    //查看全文
    $("#inquiry").on("click",".title",getContent)
  async function  getContent(){
        console.log($(this).parent().parent())
      let art_id=  $(this).parent().parent().attr("art_id");
      try{
            let result= await $.post("/getContent",{"art_id":art_id});
            if(result.status ==20000){
            console.log(result.content)
            laycontent =layer.open({
                type: 1,
                skin: 'layui-layer-rim', //加上边框
                area: ['800px', '500px'], //宽高
                shadeClose: true, 
                content: `${result.content}`
              });
          }
      }catch(err){
        laycontent =layer.open({
            type: 1,
            skin: 'layui-layer-rim', //加上边框
            area: ['800px', '1200px'], //宽高
            shadeClose: true, 
            content: `获取失败`
          });
      }
         
    }
    //编辑修改
   $("#upedit").click(function(){
       let formData = new FormData($("#editData")[0]);
       formData.append("fo_content",editor.txt.html())
       console.log(editor.txt.text())
       console.log(formData)
       $.ajax({
           url:"/update",
           type:"post",
           data:formData,
           processData: false, // 告诉jquery不需要处理数据
           contentType: false, // 不需要设置编码
       }).then( result =>{ 
           if( result.status == 20000){
            $(".equi").click()
           
            layer.msg(`${result.message}`, {icon: 1})
            $("#list_art").click()
           }
        })
       
   }) 
   //提交添加文章
   $("#addbtn").click(function(){
    let formData = new FormData($("#addart")[0]);
    $.ajax({
        url:"/addart",
        type:"post",
        data:formData,
        processData: false, // 告诉jquery不需要处理数据
        contentType: false, // 不需要设置编码
    }).then( result =>{ 
        if( result.status == 20000){
            $("#list_art").click()
            layer.msg(`${result.message}`, {icon: 1})
        }
     })
    
}) 
   

    //退出按钮
    $(".equi").click(function () {
        $(".upda").removeClass("ative").addClass("ativeout");
        setTimeout(function () {
            $(".upda").hide()
            $(".yl2").hide()
        }, 500)
    })
    //add的退出
    $(".equiadd").click(function () {
        $(".addtext").removeClass("ative").addClass("ativeout");
        $("input[type=reset]").trigger("click");
        setTimeout(function () {

            $(".addtext").hide()
            $(".yl").hide()
        }, 500)
    })
    //预览的隐藏
    $(".yl").hide()
    $(".morefile").change(function (event) {
        let a = $(this).index('.morefile')
        console.log(a)
        let file = this.files[0];
        if (file) {
            let filereader = new FileReader();
            filereader.readAsDataURL(file);
            filereader.onload = function (e) {
                $(".ylimg")[a].src = this.result;
                $(".yl").eq(a).show()
            }
        }
    })
    // document.getElementById("fo_file").onchange
    $("#hide").click(function () {
        $(".upda").hide()
    })
    function confirmRecycle(id) {
        layer.confirm('确认加入回收站吗？', {
            btn: ['确认', '非常确认'] //按钮
        }, function () {
         let sent = "recycle/?id=" + id
            ajaxsend(sent)
            layer.msg('回收站入录入', {icon: 1});
            $("#list_art").click()
        });
    }
    //永久删除
    function confirmDelete(eid, eurl) {
        layer.confirm('确认永久删除吗？', {
            btn: ['确认', '非常确认'] //按钮
        }, function () {
          let sent = "/dele?id=" + eid + "&url=" + eurl;
            // ajaxsend(sent)
         $.ajax( {
             url:"/dele",
             data: { id : eid, url:eurl }


         }).then(result => {   layer.msg('已永久删除', {icon: 1});
         $("#list_art").click() })
      
        });
    }
    $(".upimg").click(function () {
        let index = $(this).index(".upimg")
        $(".morefile").eq(index).click()
        return false;
    })

    //干饭系统
    function getCtime(aftertime){
        var atime=new Date(aftertime).getTime()
        var now=Date.now()
        var diff= (atime-now)/1000
        var content;
        if(diff<=0 &&diff>-60*10){
            content="干饭了!!!!! 干饭了!"
            return content;
        }else if(diff < -60*10){
            content="好好学习,天天干饭"
            clearInterval(DS)
            return content;
        }
        var minute=60;
        var hour=60*60;
        var day=24*60*60;
        var month=30*24*60*60;
        var monthC ;
        diff<3000? monthC=0:monthC=diff/month
        var dayC=(diff/day)%30;
        var hourC=(diff/hour)%24;   
        hourC= parseInt(hourC)
        var minuteC=(diff/minute)%60;
        var second=diff%60
        var hm=diff*100%60;
        let arr=[monthC,dayC,null,minuteC,second,hm].map(item=>{
            item=parseInt(item);
            return item.toString().padStart(2,0)      
        })
         content= "还有"+hourC+"小时"+arr[3]+"分"+arr[4]+"秒"+arr[5]+"就可以干饭了!!!!"
       return content;
    }
    let nowDate= new Date;
     let nowhour= nowDate.getHours()
   let  nowday =nowDate.getDate();
   var DS;
    $("#list_eat").click(function(){
        $(".equiadd").click()
        $(".l > ul>li").removeClass("pitch")
        $(this).addClass("pitch")
        
        $("#Articlelist").hide()
        $("#ganfan").show()
        $(".recycle").hide()
        $("#classify").hide()
 DS= setInterval(function(){
        if(nowhour <12){
            $(".ganfan").html(getCtime(`2021-9-${nowday} 12:00:00`))

        }else if(nowDate.getDay() == 4){
           
         $(".ganfan").html(getCtime(`2021-9-${nowday} 17:00:00`))
        
            
        }else{
            $(".ganfan").html(getCtime(`2021-9-${nowday} 17:30:00`))
        }
        
    },50)
    })
    //文章列表
    $('#list_art').on('click', function(){
        ii = layer.load();
        NProgress.start();
     });
    $("#list_art").click(function(){
        $(".l > ul>li").removeClass("pitch")
        $(".equiadd").click()
        $(this).addClass("pitch")
        clearInterval(DS)
        $("#classify").hide()
        $("#Articlelist").show()
        $("#ganfan").hide()
        $(".recycle").hide()
        //主体内容
        $(".mainbody").hide();
        $("input[name=catArt]").val("");
      $( "#search02").val("");
        $("#search01").click();
        layer.close(ii);
        NProgress.done();
        
    })  
    //回收站

    $('#list_recyclework').on('click', function(){
         ii = layer.load();
         NProgress.done();
          //主体内容
        $(".mainbody").hide()
      });
    $("#list_recyclework").click(function(){
        $(".equiadd").click()
        $(".l > ul>li").removeClass("pitch")
        $("#classify").hide()
        $(this).addClass("pitch")
        $(".recycle").show()
        $("#Articlelist").hide()
        $("#ganfan").hide()
        clearInterval(DS)
        let xhr=new XMLHttpRequest();
        xhr.onreadystatechange=function(){
            // console.log("123")
            if(this.readyState == 4 && this.status ==200){
                layer.close(ii);
                
            NProgress.done()
                  var ea=  this.responseText
                let start =ea.search(/<table/)
                let end =ea.search(/<\/table>/)+8
                var tt=ea.substring(start,end)
                $(".searchRecycle").html(tt)
       
            }
        }
        xhr.open("get","/recyclework",true)

           xhr.send()

    })
    //永久删除
function confirmDelete(eid,eurl){
    layer.confirm('确认永久删除吗？', {
  btn: ['确认','非常确认'] //按钮
}, function(){
$.ajax( {
    url:"/dele",
    data: { id : eid, url:eurl }
}).then(result => {   layer.msg('已永久删除', {icon: 1});
$("#list_recyclework").click() })
});
}
function confirmReback(id,url){
    layer.confirm('确认还原吗？', {
  btn: ['确认','非常确认'] //按钮
}, function(){
//   location.href="/Reback?id="+id;
 let  sent="/Reback?id="+id;
//  ajaxsend(sent)

 layer.msg('还原成功', {icon: 1});
 $("#list_recyclework").click()

});
}
//classify分类管理
$('#list_classify').on('click', function(){
    ii = layer.load();
    NProgress.start();
     //主体内容
     $(".mainbody").hide();
 });
$("#list_classify").click(function(){
    $(".equiadd").click()
    $(".l > ul>li").removeClass("pitch")
    $(this).addClass("pitch")
    $(".recycle").hide()
    $("#Articlelist").hide()
    $("#ganfan").hide()
    clearInterval(DS)
    $("#classify").show()
    let xhr=new XMLHttpRequest();
    xhr.onreadystatechange=function(){
        if(this.readyState == 4 && this.status ==200){
            layer.close(ii);
            
            NProgress.done()
              let ea=  this.responseText;
              if(!ea.search(/<!DOCTYPE html>/)){
                location.href="/"
            }else{
                $("#classify").html(ea)
            }
        }
    }
    xhr.open("get","/classify",true)

       xhr.send()
    
})
//删除分类条
$("#classify").on("click","#cate_dele",cate_delt)
function cate_delt(){
    let id= $(this).attr("edit_id")
    $.ajax({
        url:"/deleteCate",
        type: "POST",
        data: {edit_id:id},
        success: function(msg){
            if(msg.status ==20000)
            layer.msg(`删除成功`, {icon: 1})
            $("#list_classify").click()

        }
    })
}

//分类条
$("#classify").on("click","#cate_edit",cate_edit)
var editid;
function cate_edit(){
      editid= $(this).attr("edit_id");
    let val1 =$(this).parent().parent().children().eq(1).text();
    let val2 =$(this).parent().parent().children().eq(2).text();
    var e="",c="";
    if(val2.replace(/\s/g,"")=="显示"){
         e= "checked"
    }else{
        c="checked"
    }
    console.log(val1.replace(/\s/g,""))
    addclaaify=layer.open({
        type: 1,
        skin: 'layui-layer-rim', //加上边框
        area: ['420px', '388px'], //宽高
        shadeClose: true, 
        content: `   
    <form class="layui-form" action="">
  <div class="layui-form-item">
    <label class="layui-form-label">修改分类</label>
    <div class="layui-input-inline">
      <input type="text" name="classify3" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
  <label class="layui-form-label">是否隐藏</label>
  <input type="radio" lay-filter="classifyshow" name="is_show" value="0" title="隐藏" ${c}>
    <input type="radio" lay-filter="classifyshow" name="is_show" value="1" title="显示" ${e}>
  </div>
    <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="classifyupdata">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
  </div>
</div>
</form>
        `
      });
      $("button[type=reset]").click();
      $("input[name=classify3]").val(val1.replace(/\s/g,""))
      
  
}




$("#list_add").click(function(){
    $(".add").click()
})

//点击更换头像
function changeurl(){
  $(".changeurl").click();
}
//监听更换头像是否有上传文件
$(".changeurl").change(function(){
    console.log(this.files)
    let file = this.files[0];
    if(!file){
        return ;
    }
    // console.log(file)
    let formData = new FormData();
    formData.append("changeurl",file)
    let xhr =new XMLHttpRequest();
    xhr.onreadystatechange=function(){
        if(this.readyState === 4 && this.status ===200){
            let result =JSON.parse( this.responseText);
            console.log(result)
            //返回接收结果,信息, avurl
            if(result.status =="10001"){
                return;
            }else if(result.status =="10000"){
                $("#avurl")[0].src = result.avurl;
                layer.msg(`${result.message}`, {icon: 1})
            }
           
        }
    }
    xhr.open('post',"/updataurl",true)
    xhr.send(formData)



})
//修改密码弹出层
var openpass;
function updatapassword(){
    openpass=layer.open({
        type: 1,
        skin: 'layui-layer-rim', //加上边框
        area: ['420px', '388px'], //宽高
        shadeClose: true, 
        content: `
    <form class="layui-form" action="">
  <div class="layui-form-item">
    <label class="layui-form-label">原密码</label>
    <div class="layui-input-inline">
      <input type="password" name="password1" required  lay-verify="required" placeholder="请原密码" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">新密码</label>
    <div class="layui-input-inline">
      <input type="password2" name="password2" required lay-verify="required" placeholder="请输入新密码" autocomplete="off" class="layui-input">
    </div>
    
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">确认密码</label>
    <div class="layui-input-inline">
     
    <input type="password" name="password3" required lay-verify="required" placeholder="请确认密码" autocomplete="off" class="layui-input">
   
    </div>
    <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="changepass">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
  </div>
</div>

 
</form>
        
        `
      });
    
}
//添加分类 
 $("#list_classifyAdd").click(function(){
    addclaaify=layer.open({
        type: 1,
        skin: 'layui-layer-rim', //加上边框
        area: ['420px', '388px'], //宽高
        shadeClose: true, 
        content: `
        
    <form class="layui-form" action="">
  <div class="layui-form-item">
    <label class="layui-form-label">添加分类</label>
    <div class="layui-input-inline">
      <input type="text" name="classify3" required  lay-verify="required" placeholder="新分类" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
  <label class="layui-form-label">是否隐藏</label>
  <input type="radio" name="is_show" value="0" title="隐藏">
<input type="radio" name="is_show" value="1" title="显示" checked>
  </div>
    <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="classifyAdd">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
  </div>
</div>
</form>
        
        `
      });
    // $("button[type=reset]").trigger("click");
    $("button[type=reset]").click();
  
 }) 

function ajaxsend(lujin){
    let xhr=new XMLHttpRequest();
    xhr.onreadystatechange=function(){
        if(this.readyState == 4 && this.status ==200){
            if(this.responseText =="接收了"){
                console.log("接收了")
            }
        }
    }
    xhr.open("get",lujin,true)
    //    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xhr.send()
}